import { defineConfig, loadEnv } from 'vite';
import alias from './vite/alias'
import { parseEnv } from './vite/util'
import setupPlugins from './vite/plugins'
import vueJsx from '@vitejs/plugin-vue-jsx'

export default defineConfig(({ command, mode }) => {
  const isBuild = command == 'build'
  const env = parseEnv(loadEnv(mode, process.cwd()))

  // console.log(alias, 'alias')
  return {
    plugins: [...setupPlugins(isBuild, env), vueJsx({})],
    //静态文件 url 前缀
    base: env.VITE_PUBLIC_PATH,
    resolve: {
      alias
    },
    css: {
      preprocessorOptions: {
        // css 全局变量
        scss: {
          additionalData: '@import "@/styles/variables.scss";'
        }
      }
    },
    build: {
      //编译文件生成目录
      outDir: './dist',
      emptyOutDir: true,
      rollupOptions: {
        output: {
          manualChunks(id: string) {
            if (id.includes('node_modules')) {
              return id.split('/node_modules/').pop()?.split('/')[0]
            }
          },
        },
      },
    },
    server: {
      host: true,
      port: 7777,
      proxy: {
        '/api': {
          target: env.VITE_APP_BASE_API,
          changeOrigin: true,
        },
        '/captcha/api/math': {
          target: env.VITE_APP_BASE_API,
          changeOrigin: true,
        },
      },
    },
  }
})
